package cn.itsource.aigou.service.impl;

import cn.itsource.aigou.domain.ProductType;
import cn.itsource.aigou.mapper.ProductTypeMapper;
import cn.itsource.aigou.service.IProductTypeService;
import cn.itsource.common.RedisClient;
import cn.itsource.common.StaticPageClient;
import cn.itsource.util.AjaxResult;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import springfox.documentation.spring.web.json.Json;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品目录 服务实现类
 * </p>
 *
 * @author colta
 * @since 2019-07-30
 */
@Service
public class ProductTypeServiceImpl extends ServiceImpl<ProductTypeMapper, ProductType> implements IProductTypeService {

    /*此接口集成lOpenfeign  所以会生成代理类交给Spring管理*/
    @Autowired
    private RedisClient redisClient;
    @Autowired
    private StaticPageClient staticPageClient;

    @Override
    public void getHomePage() {
        Map<String, Object> map = new HashMap<>();
        /*s生成已经结合数据的vm.html文件*/
        map.put("model",loadTypeTree());
        map.put("templatePathAndName","E:\\IDEAworkspace\\aigou-parent\\aigou-product-parent\\aigou-product-service\\src\\main\\resources\\template\\product.type.vm");
        map.put("targetPathAndName","E:\\IDEAworkspace\\aigou-parent\\aigou-product-parent\\aigou-product-service\\src\\main\\resources\\template\\product.type.vm.html");
        staticPageClient.geneStaticPage(map);
        /*生成目标html文件*/
        map = new HashMap<>();

        map.put("templatePathAndName","E:\\IDEAworkspace\\aigou-parent\\aigou-product-parent\\aigou-product-service\\src\\main\\resources\\template\\home.vm");
        map.put("targetPathAndName","E:\\IDEAworkspace\\aigou-web-parent\\aigou-web-home\\home.html");

        Map<String, String> model = new HashMap<>();
        model.put("staticRoot","E:\\IDEAworkspace\\aigou-parent\\aigou-product-parent\\aigou-product-service\\src\\main\\resources\\");
        map.put("model",model);
        staticPageClient.geneStaticPage(map);
    }

    @Override
    public List<ProductType> loadTypeTree() {

        AjaxResult ajaxResult = redisClient.get("productTypes");
        String productTypesJsonStr = (String)ajaxResult.getResultObj();
        List<ProductType> productTypes = JSON.parseArray(productTypesJsonStr, ProductType.class);
        if (productTypes==null||productTypes.size()<=0){
            productTypes = loop();
            redisClient.set("productTypes",JSON.toJSONString(productTypes));
        }
        return productTypes;
    }

    public List<ProductType> loop() {
        List<ProductType> productTypes = baseMapper.selectList(null);
        List<ProductType> parents = new ArrayList<>();
        HashMap<Long, ProductType> map = new HashMap<>();
        for (ProductType productType : productTypes) {
            map.put(productType.getId(),productType);
        }
        for (ProductType productType : productTypes) {
            if (productType.getPid() == 0) {
                parents.add(productType);
            }else {
                ProductType parent = map.get(productType.getPid());
                List<ProductType> children = parent.getChildren();
                /*如果children集合空*/
                if (children==null){
                    children = new ArrayList<>();
                }
                children.add(productType);
                parent.setChildren(children);
            }
        }
        return parents;
    }
}
